package com.xyauto.theme.service

import android.app.Service
import android.content.Intent
import android.graphics.PixelFormat
import android.os.IBinder
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import com.blankj.utilcode.util.LogUtils
import com.xyauto.theme.BuildConfig
import com.xyauto.theme.R
import com.xyauto.theme.bean.ISubResBean
import com.xyauto.theme.bean.SubAnimationBean
import com.xyauto.theme.utils.AutoUtils
import com.xyauto.theme.utils.ThemeFileManager
import com.xyauto.theme.view.AppListViewHolder
import kotlinx.coroutines.*


class AppListService : Service() {
    companion object {
        const val ACTION_SHOW_FLOATING_VIEW = "com.xyauto.theme.AppListConfig"
        const val ACTION_CLOSE_FLOATING_VIEW = "com.xyauto.theme.AppListConfig.close"
    }

    override fun onBind(intent: Intent?): IBinder? = null

    private lateinit var windowManager: WindowManager
    private lateinit var floatingLayoutParams: WindowManager.LayoutParams
    private lateinit var floatingView: AppListViewHolder
    private var isShow: Boolean = false
    private var mCoroutineScope: CoroutineScope? = null

    override fun onCreate() {
        super.onCreate()
        LogUtils.i("onCreate: ")
        windowManager = application.getSystemService(WINDOW_SERVICE) as WindowManager;
        floatingLayoutParams = WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, // 悬浮窗类型为TYPE_APPLICATION_OVERLAY
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // 不获取焦点
            PixelFormat.TRANSLUCENT // 透明窗口
        )
        floatingLayoutParams.gravity = Gravity.BOTTOM
        floatingLayoutParams.width = AutoUtils.displayWidth
        floatingLayoutParams.height = AutoUtils.displayHeight - AutoUtils.getStatusBarHeight(this)
        floatingView = AppListViewHolder(LayoutInflater.from(this).inflate(R.layout.service_app_list, null, false))

    }


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (BuildConfig.SHOW_SERVICE_DIALOG) {
            val action = intent?.action ?: ""
            if (TextUtils.equals(ACTION_SHOW_FLOATING_VIEW, action)) {
                getList {
                    if (!isShow) {
                        isShow = true
                        windowManager.addView(floatingView.itemView, floatingLayoutParams)
                    } else {
                        windowManager.updateViewLayout(floatingView.itemView, floatingLayoutParams)
                    }
                    floatingView.loadIndex()
                }
            } else if (TextUtils.equals(ACTION_CLOSE_FLOATING_VIEW, action)) {
                if (isShow) {
                    isShow = false
                    windowManager.removeView(floatingView.itemView)
                }
            }
        }
        return super.onStartCommand(intent, flags, startId)
    }


    private fun getList(block: suspend () -> Unit) {
        if (mCoroutineScope == null) {
            mCoroutineScope = CoroutineScope(Dispatchers.Main).apply {
                launch {
                    floatingView.setAnimationData(
                        arrayListOf(
                            SubAnimationBean(getString(R.string.anim_type_no_animation), R.drawable.animic_type_none, 0),
                            SubAnimationBean(getString(R.string.anim_type_scale), R.drawable.animic_type_scale, 1),
                            SubAnimationBean(getString(R.string.anim_type_left_fade), R.drawable.animic_type_left_fade, 2),
                            SubAnimationBean(getString(R.string.anim_type_right_fade), R.drawable.animic_type_right_fade, 3),
                            SubAnimationBean(getString(R.string.anim_type_large_fade), R.drawable.animic_type_large_fade, 4),
                            SubAnimationBean(getString(R.string.anim_type_small_fade), R.drawable.animic_type_small_fade, 5),
                            SubAnimationBean(getString(R.string.anim_type_up_rotate), R.drawable.animic_type_up_rotate, 8),
                            SubAnimationBean(getString(R.string.anim_type_down_rotate), R.drawable.animic_type_down_rotate, 9),
                            SubAnimationBean(getString(R.string.anim_type_compress), R.drawable.animic_type_compress, 10),
                            SubAnimationBean(getString(R.string.anim_type_book_turn), R.drawable.animic_type_book_trun, 11),
                            SubAnimationBean(getString(R.string.anim_type_flap), R.drawable.animic_type_flap, 12),
                            SubAnimationBean(getString(R.string.anim_type_cube), R.drawable.animic_type_cube, 13),
                            SubAnimationBean(getString(R.string.anim_type_gallery), R.drawable.animic_type_gallery, 14),
                            SubAnimationBean(getString(R.string.anim_type_cut_cards), R.drawable.animic_type_cut_cards, 15)
                        )
                    )
                    val iconList: MutableList<ISubResBean> = withContext(Dispatchers.IO) {
                        ThemeFileManager.scanIconResByPath(BuildConfig.DEFAULT_RES_PATH)
                    }
                    floatingView.setIconData(iconList)
                    block.invoke()
                }
            }
        } else {
            mCoroutineScope?.launch { block.invoke() }
        }
    }

    override fun onDestroy() {
        mCoroutineScope?.cancel()
        mCoroutineScope = null
        if (isShow) {
            isShow = false
            windowManager.removeView(floatingView.itemView)
        }
        super.onDestroy()
    }

}